package com.inspur.myapplication.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inspur.myapplication.entity.UserBean;
import com.inspur.myapplication.util.ShoppingDbHelper;
import com.inspur.myapplication.view.LoginActivity;

/**
 * Created by Administrator on 2020/11/18.
 */

public class UserDao {
    private Context context;
    public UserDao(Context context){
        this.context=context;
    }

    public UserBean checkLogin(UserBean userBean){
        String userName=userBean.getUserName();
        String password=userBean.getPassword();
        UserBean rst=null;


        ShoppingDbHelper shoppingDbHelper=new ShoppingDbHelper(context);
        SQLiteDatabase sqLiteDatabase=shoppingDbHelper.getReadableDatabase();
        try {
            Cursor cursor = sqLiteDatabase.query("userInfotb", new String[]{"userid", "username", "password"}, "username=? and password=?", new String[]{userName, password}, null, null, null);
            while (cursor.moveToNext()) {
                rst = new UserBean();
                String id = cursor.getString(cursor.getColumnIndex("userid"));
                userName = cursor.getString(cursor.getColumnIndex("username"));
                password = cursor.getString(cursor.getColumnIndex("password"));

                rst.setUserId(id);
                rst.setPassword(password);
                rst.setUserName(userName);
            }
            cursor.close();
        }catch (Exception e){
            Log.e(LoginActivity.class.toString(),e.toString());
        }finally {
            sqLiteDatabase.close();
        }

        return rst;
    }
}
